package com.atguigu.guli.service.oss.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.oss.service.FileService;
import com.atguigu.guli.service.oss.util.OssProperties;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {
    @Autowired
    OssProperties ossProperties;
    // Endpoint以杭州为例，其它Region请按实际情况填写。
    String schema;
    String endpoint;  //为什么不直接设置成员变量，因为所用的properties类是注入的，可能到设置成员变量的时候，还没有注入properties类
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录RAM控制台创建RAM账号。
    String accessKeyId;
    String accessKeySecret;
    String bucketName;

    //当前类的构造器执行完毕后，立即初始化成员变量的值
    @PostConstruct //jdk提供的 bean的构造器后立即执行的初始化方法
    public void init() {

        this.endpoint = ossProperties.getEndpoint();
        this.accessKeySecret = ossProperties.getAccessKeySecret();
        this.accessKeyId = ossProperties.getAccessKeyId();
        this.bucketName = ossProperties.getBucketName();
    }


    @Override
    public String upload(InputStream inputStream, String module, String oFilename) {
        try {

            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            if (!ossClient.doesBucketExist(bucketName)) {

                ossClient.createBucket(bucketName);

                ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
            }

            String folder = new DateTime().toString("yyyy/MM/dd");

            String fileName = UUID.randomUUID().toString();
            String fileExtension = oFilename.substring(oFilename.lastIndexOf("."));
            String key = module + "/" + folder + "/" + fileName + fileExtension;


            ossClient.putObject(bucketName, key, inputStream);

            ossClient.shutdown();

            return "https://" + bucketName + "." + endpoint + "/" + key;
        } catch (Exception e) {
            ExceptionUtils.getStackTrace(e);
            throw new GuliException(ResultCodeEnum.FILE_UPLOAD_ERROR);
        }
    }

    @Override
    public void delete(String path, String module) {
        String endpoint = ossProperties.getEndpoint();
        String keyid = ossProperties.getAccessKeyId();
        String keysecret = ossProperties.getAccessKeySecret();
        String bucketname = ossProperties.getBucketName();
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        String ObjectName = path.substring(path.lastIndexOf(module));
        ossClient.deleteObject(bucketname, ObjectName);
        ossClient.shutdown();
    }
}